import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:nyh_motorcade_app/entity/company_entity.dart';
import 'package:nyh_motorcade_app/styles/colors.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:nyh_motorcade_app/widgets/highlight_rich_text.dart';
import 'package:nyh_motorcade_app/widgets/multi_state/multi_state_widget.dart';
import 'package:nyh_motorcade_app/widgets/search_app_bar.dart';
import 'search_account_controller.dart';

class SearchAccountPage extends StatelessWidget {
  final controller = Get.put(SearchAccountController());

  @override
  Widget build(BuildContext context) {
    return GetBuilder<SearchAccountController>(
      builder: (controller) {
        return Scaffold(
          backgroundColor: Colours.color_background,
          resizeToAvoidBottomInset: false,
          appBar: SearchAppBar(
            controller: controller.searchController,
            context: context,
            hintText: "请输入账户名称/账户编号",
            onChanged: (value) {
              controller.searchAccount(value);
            },
          ),
          body: Container(
            child: Stack(
              fit: StackFit.expand,
              alignment: Alignment.center,
              children: [buildHistoryRecordWidget(), buildSearchResultWidget()],
            ),
          ),
        );
      },
    );
  }

  Widget buildHistoryRecordWidget() {
    return Visibility(
      visible: controller.keyword.isEmpty,
      child: Positioned(
        left: 0,
        top: 0,
        right: 0,
        bottom: 0,
        child: Column(
          children: [
            Container(
              height: 40.w,
              padding: EdgeInsets.only(left: 16.w),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  Text(
                    "历史搜索",
                    style: TextStyle(
                      color: Colours.color_text_theme,
                      fontSize: 14.sp,
                      fontWeight: FontWeight.bold,
                    ),
                  ),
                  Visibility(
                    visible: controller.historyRecordList.isNotEmpty,
                    child: IconButton(
                      onPressed: () {
                        controller.deleteHistoryRecord();
                      },
                      icon: Image.asset("images/ic_delete.png"),
                    ),
                  ),
                ],
              ),
            ),
            Expanded(
              child: ListView.separated(
                shrinkWrap: true,
                physics: BouncingScrollPhysics(),
                itemBuilder: (context, index) {
                  return buildHistoryRecordItemWidget(index);
                },
                separatorBuilder: (context, index) {
                  return Divider(color: Colours.color_background, height: 1.w, thickness: 1.w);
                },
                itemCount: controller.historyRecordList.length,
              ),
            ),
          ],
        ),
      ),
    );
  }

  Widget buildHistoryRecordItemWidget(int index) {
    String string = controller.historyRecordList[index];
    return Material(
      child: InkWell(
        onTap: () {
          controller.searchController.text = string;
          controller.searchAccount(string);
        },
        child: Padding(
          padding: EdgeInsets.symmetric(vertical: 17.w, horizontal: 16.w),
          child: Text(
            string,
            style: TextStyle(color: Colours.color_text_theme, fontSize: 14.sp),
            maxLines: 1,
            overflow: TextOverflow.ellipsis,
          ),
        ),
      ),
    );
  }

  Widget buildSearchResultWidget() {
    return Visibility(
      visible: controller.keyword.isNotEmpty,
      child: Positioned(
        left: 0,
        top: 0,
        right: 0,
        bottom: 0,
        child: MultiStateWidget(
          child: Column(
            children: [
              Container(
                height: 40.w,
                padding: EdgeInsets.only(left: 16.w),
                child: Row(
                  children: [
                    Text(
                      "相关账户(${controller.companySearchList.length})",
                      style: TextStyle(
                        color: Colours.color_text_theme,
                        fontSize: 14.sp,
                        fontWeight: FontWeight.bold,
                      ),
                    ),
                  ],
                ),
              ),
              Expanded(
                child: ListView.builder(
                  shrinkWrap: true,
                  physics: BouncingScrollPhysics(),
                  itemCount: controller.companySearchList.length,
                  itemBuilder: (context, index) {
                    return buildSearchResultItemWidget(index);
                  },
                ),
              ),
            ],
          ),
          state: controller.multiState,
        ),
      ),
    );
  }

  Widget buildSearchResultItemWidget(int index) {
    CompanyEntity companyEntity = controller.companySearchList[index];
    return Container(
      margin: EdgeInsets.only(left: 16.w, top: 0, right: 16.w, bottom: 12.w),
      decoration: BoxDecoration(
        color: Colours.color_white,
        borderRadius: BorderRadius.circular(10.w),
      ),
      child: ClipRRect(
        borderRadius: BorderRadius.circular(10.w),
        child: Material(
          child: InkWell(
            onTap: () {
              controller.backResult(companyEntity.id ?? "", companyEntity.name ?? "", companyEntity.code ?? "");
            },
            child: Padding(
              padding: EdgeInsets.all(16.w),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  HighlightRichText(
                    text: companyEntity.name ?? "",
                    highlightList: [controller.keyword],
                    highlightStyle: TextStyle(
                      color: Colours.color_theme,
                      fontSize: 16.sp,
                      fontWeight: FontWeight.bold,
                    ),
                    normalStyle: TextStyle(
                      color: Colours.color_text_theme,
                      fontSize: 16.sp,
                      fontWeight: FontWeight.bold,
                    ),
                  ),
                  SizedBox(
                    height: 5.w,
                  ),
                  HighlightRichText(
                    text: companyEntity.code ?? "",
                    highlightList: [controller.keyword],
                    highlightStyle: TextStyle(
                      color: Colours.color_theme,
                      fontSize: 12.sp,
                    ),
                    normalStyle: TextStyle(
                      color: Colours.color_b1b2bb,
                      fontSize: 12.sp,
                    ),
                    maxLines: 1,
                    overflow: TextOverflow.ellipsis,
                  ),
                ],
              ),
            ),
          ),
        ),
      ),
    );
  }
}
